package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.AlarmIndicatorBo;
import org.dromara.system.domain.vo.AlarmIndicatorVo;
import org.dromara.system.executor.DataExecutorService;
import org.dromara.system.service.IAlarmIndicatorService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 监测点位
 *
 * @author Lion Li
 * @date 2025-10-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/indicator")
public class AlarmIndicatorController extends BaseController {

    private final IAlarmIndicatorService alarmIndicatorService;

    private final DataExecutorService dataExecutorService;


    /**
     * 获取监测点位列表
     */

    @GetMapping("/get/indicators")
    public R<List<String>> getIndicatorNums() {
        List<String> indicatorNums = alarmIndicatorService.getIndicatorNums();
        return R.ok(indicatorNums);
    }

    /**
     * 查询监测点位列表
     */
    @SaCheckPermission("system:indicator:list")
    @GetMapping("/list")
    public TableDataInfo<AlarmIndicatorVo> list(AlarmIndicatorBo bo, PageQuery pageQuery) {
        return alarmIndicatorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出监测点位列表
     */
    @SaCheckPermission("system:indicator:export")
    @Log(title = "监测点位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AlarmIndicatorBo bo, HttpServletResponse response) {
        List<AlarmIndicatorVo> list = alarmIndicatorService.queryList(bo);
        ExcelUtil.exportExcel(list, "监测点位", AlarmIndicatorVo.class, response);
    }

    /**
     * 获取监测点位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:indicator:query")
    @GetMapping("/{id}")
    public R<AlarmIndicatorVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(alarmIndicatorService.queryById(id));
    }

    /**
     * 新增监测点位
     */
    @SaCheckPermission("system:indicator:add")
    @Log(title = "监测点位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AlarmIndicatorBo bo) {
        return toAjax(alarmIndicatorService.insertByBo(bo));
    }

    /**
     * 修改监测点位
     */
    @SaCheckPermission("system:indicator:edit")
    @Log(title = "监测点位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AlarmIndicatorBo bo) {
        return toAjax(alarmIndicatorService.updateByBo(bo));
    }

    /**
     * 删除监测点位
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:indicator:remove")
    @Log(title = "监测点位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(alarmIndicatorService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 推送数据到数据平台
     */
    @GetMapping("/push/indicators/data-platform")
    public R<Boolean> pushToDataFromDataPlatform() {
        dataExecutorService.pushToDataFromDataPlatform();
        return R.ok(true);
    }
}
